﻿using System.Web;

namespace XwOa
{
    internal class XwOaHelper
    {
        private static readonly string EventValidation = HttpUtility.UrlEncode("/wEWBALz1fTcCQKMrfuqAgKOsPfSCQLv8O2RCrH4k9ON8qZdsobpFm+S5OSwv0k7P/ZdecHrX4QZYejk");

        private static readonly string ViewState = HttpUtility.UrlEncode("/wEPDwUKMTA1NzU3NjE1MWRk9wD9xaAVYev7mO9WMBBlr40fTKCPUp0bCU9f+a/bMUA=");

        /// <summary>
        /// OA登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回是否登录成功</returns>
        //public static bool Login(string userName, string password)
        //{
        //    try
        //    {
        //        string strArgs =
        //            string.Format(
        //                "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&ctl00%24MainContent%24txtUserName={2}&ctl00%24MainContent%24txtPassword={3}&ctl00%24MainContent%24LoginButton=%E7%99%BB%E5%BD%95",
        //                ViewState, EventValidation, userName, password);
        //        return !WebHelper.PostLogin("http://v6.666gps.com:88/Login.aspx", strArgs).Contains("用户或密码错误！");
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回是否签到成功</returns>
        public static bool SignIn(string userName, string password)
        {
            if (!Login(userName, password))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 签退
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回是否签退成功</returns>
        public static bool SignOut(string userName, string password)
        {
            if (!Login(userName, password))
            {
                return false;
            }

            return true;
        }

        public static bool Login(string userName, string password)
        {
            try
            {
                string page = WebHelper.GetPage("http://v6.666gps.com:88/Login.aspx");
                string str = WebHelper.GetValueById(page, "__EVENTVALIDATION");
                string str5 = WebHelper.GetValueById(page, "__VIEWSTATE");
                string strArgs =
                    string.Format(
                        "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&ctl00%24MainContent%24txtUserName={2}&ctl00%24MainContent%24txtPassword={3}&ctl00%24MainContent%24LoginButton=%E7%99%BB%E5%BD%95",
                        HttpUtility.UrlEncode(str5), HttpUtility.UrlEncode(str), userName, password);
                if (!WebHelper.PostLogin("http://v6.666gps.com:88/Login.aspx", strArgs).Contains("用户或密码错误！"))
                {
                    return true;
                }
            }
            catch
            {
            }

            return false;
        }
    }
}